<template>
  <div id="app">
    <div id="up_div">
      <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="操作类型" prop="operationType">
            <el-radio-group v-model="queryParams.operationType"  @change="handleQuery">
              <el-radio-button
                v-for="item in operationType"
                :key="item.value"
                :label="item.value"
              >
                {{ item.label }}
              </el-radio-button>
            </el-radio-group>
          </el-form-item>
      <el-form-item label="订单编号" prop="operationNo">
        <el-input
          v-model="queryParams.operationNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产日期" prop="productionTime">
        <el-date-picker clearable
          v-model="queryParams.productionTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择生产日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="过期日期" prop="expirationTime">
        <el-date-picker clearable
          v-model="queryParams.expirationTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择过期日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
      </div>
    </div>
    <div id="down_div">
      <div class="app-container">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:wmsInventoryHistory:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="wmsInventoryHistoryList" >
          <el-table-column label="订单编号" align="center" prop="operationNo" />
          <el-table-column label="操作类型" align="center" prop="operationType">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.wms_operation_type" :value="scope.row.operationType"/>
            </template>
          </el-table-column>
          <el-table-column label="商品名称" align="center" prop="productName" />
          <el-table-column label="商品规格" align="center" prop="productSpec" />
          <el-table-column label="数量" align="center" prop="totalQuantity" />
          <el-table-column label="仓库信息" align="center" width="280">
            <template slot-scope="scope">
              <div class="warehouse-display">
                <!-- 仓库和库区行 -->
                <div class="warehouse-line">
                  <span class="label-value"><strong>仓库:</strong>{{ getSelectLabel(warehouseSelect, scope.row.warehouseId) || '-' }}</span>
                  <span class="separator" v-if="scope.row.warehouseAreaId">|</span>
                  <span class="label-value"><strong>库区:</strong>{{ getSelectLabel(warehouseAreaSelect, scope.row.warehouseAreaId) || '-' }}</span>
                </div>

                <!-- 货架和货位行 -->
                <div class="warehouse-line" v-if="scope.row.warehouseRackId || scope.row.warehouseLocationId">
                  <span class="label-value"><strong>货架:</strong>{{ getSelectLabel(warehouseRackSelect, scope.row.warehouseRackId) || '-' }}</span>
                  <span class="separator" v-if="scope.row.warehouseLocationId">|</span>
                  <span class="label-value"><strong>货位:</strong>{{ getSelectLabel(warehouseLocationSelect, scope.row.warehouseLocationId) || '-' }}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="生产日期" align="center" prop="productionTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.productionTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="过期日期" align="center" prop="expirationTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.expirationTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>

  </div>
</template>

<script>
import { listWmsInventoryHistory } from "@/api/system/wmsInventoryHistory"
import { getSupplierSelect,
  getWarehouseAreaSelect,
  getWarehouseSelect,
  getWarehouseRackSelect,
  getWmsWarehouseLocationSelect }  from "@/api/system/selectVo"

export default {
  name: "WmsInventoryHistory",
  dicts: ['wms_operation_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存记录表格数据
      wmsInventoryHistoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        operationId: null,
        operationNo: null,
        operationType: "",
        productName: null,
        totalQuantity: null,
        totalAmount: null,
        productionTime: null,
        expirationTime: null,
      },
      operationType:[
        {
          "value":"",
          "label":"全部"
        },
        {
          "value":"1",
          "label":"入库"
        },
        {
          "value":"2",
          "label":"出库"
        },
        {
          "value":"3",
          "label":"移库"
        },
        {
          "value":"4",
          "label":"盘库"
        },
      ],
      warehouseAreaSelect:[],
      warehouseSelect:[],
      warehouseRackSelect:[],
      warehouseLocationSelect:[],
    }
  },
  created() {
    this.getSelect();
    this.getList();
  },
  methods: {
    /** 查询库存记录列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0]
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1]
      }
      listWmsInventoryHistory(this.queryParams).then(response => {
        this.wmsInventoryHistoryList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        operationId: null,
        operationNo: null,
        operationType: "",
        productId: null,
        totalQuantity: null,
        totalAmount: null,
        productionTime: null,
        expirationTime: null,
        createTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/wmsInventoryHistory/export', {
        ...this.queryParams
      }, `wmsInventoryHistory_${new Date().getTime()}.xlsx`)
    },
    getSelectLabel(selectList, id) {
      if (!selectList || !id) return '-'
      const item = selectList.find(i => i.value === id)
      return item ? item.label : (id || '-')
    },
    //获取下关的下拉框
    getSelect(){
      getSupplierSelect().then(response => {
        this.supplierSelect = response.data
      });
      getWarehouseAreaSelect().then(response => {
        this.warehouseAreaSelect = response.data
      });
      getWarehouseSelect().then(response => {
        this.warehouseSelect = response.data
      });
      getWarehouseRackSelect().then(response => {
        this.warehouseRackSelect = response.data
      });
      getWmsWarehouseLocationSelect().then(response => {
        this.warehouseLocationSelect = response.data
      });
    },
  }
}
</script>
<style>
body, html {
  margin: 0;
  padding: 0;
  height: 100%;
}

#app {
  //background-color: #f4f4f4;
  background-color: #ffffff;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 3px;
}

#up_div {
  background-color: white;
  height: 30%;
  width: 100%;
  margin-bottom: 20px; /* Space between the two divs */
  box-sizing: border-box;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  border-radius: 4px; /* Optional rounded corners */
}

#down_div {
  background-color: white;
  height: 70%;
  width: 100%;
  box-sizing: border-box;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  border-radius: 4px; /* Optional rounded corners */
}

.warehouse-display {
  font-size: 12px;
  line-height: 1.5;
}

.warehouse-line {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}

.label-value {
  padding: 0 4px;
  white-space: nowrap;
}

.separator {
  color: #c0c4cc;
  padding: 0 4px;
}
</style>
